package com.karaoke.android.kege.fragment


import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.transition.Slide
import android.transition.Transition
import android.transition.TransitionInflater
import android.view.Gravity
import com.karaoke.android.data.domain.Singer
import com.karaoke.android.data.onUI
import com.karaoke.android.kege.BR
import com.karaoke.android.kege.CacheBitmap
import com.karaoke.android.kege.KegeActivity
import com.karaoke.android.kege.R
import com.karaoke.android.kege.databinding.FragmentSingerDetailBinding
import com.karaoke.android.kege.vm.SingerDetailViewModel
import com.karaoke.android.library.mvvm.BindingConfig
import kotlin.properties.Delegates


/**
 * A simple [Fragment] subclass.
 */
class SingerDetailFragment : BaseFragment<SingerDetailViewModel>()
{

    companion object
    {
        fun newInstance(singer: Singer) = SingerDetailFragment().apply { this.singer = singer }
    }

    var singer: Singer by Delegates.notNull()
    override val viewModel: SingerDetailViewModel by lazy { SingerDetailViewModel(singer) }

    override val config: BindingConfig by lazy { BindingConfig(R.layout.fragment_singer_detail, BR.viewModel) }

    override fun getEnterTransition(): Transition
    {
        return Slide(Gravity.BOTTOM).apply {
            duration = 200
            addTarget(R.id.srInfo)
            addTarget(R.id.singerInfo)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        title = "歌星 - " + singer.name
    }

    override fun getSharedElementEnterTransition(): Transition
    {
        return TransitionInflater.from(KegeActivity.activity)
                .inflateTransition(R.transition.shared_explode)
    }


    override fun onInitLoad()
    {
        super.onInitLoad()
        val mBinding = dataBinding as FragmentSingerDetailBinding
//        if (singer.smallPicUrl != null)
//            activity.CacheBitmap(Uri.parse("http://main.oksrv.com" + singer.smallPicUrl))
//                    .filter { it != null }
//                    .onUI { mBinding.avatar.setimage) }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?)
    {
        super.onActivityCreated(savedInstanceState)
    }

    override fun onDestroyView()
    {
        viewModel.position.set((dataBinding as FragmentSingerDetailBinding).gridView.firstVisiblePosition)
        super.onDestroyView()
    }
}
